<?php

class Log{
	
	protected $_errFile = null;
	
	protected $_accFile = null;
	
	protected $_debFile = null;
	
	protected $_sucFile = null;
	public function __construct(){
		$configClass   = new Config();
		$this->_errFile = $configClass->getKey("errorLogFile")->get();
		$this->_accFile = $configClass->getKey("accessLogFile")->get();
		$this->_debFile = $configClass->getKey("debugLogFile")->get();
		$this->_sucFile = $configClass->getKey("successLogFile")->get();
	}
	/**
	 * 写入字符串类型日志
	 * @param unknown_type $file
	 * @param unknown_type $msg
	 */
	protected function log($file,$msg){	
		
		$fp   = fopen($file,"a");//文件末尾;		
		fwrite($fp,$msg."\n");
		fclose($fp);
	}
	
	protected function mulLog($file,$message){
		$message  = is_array($message)?$this->arrToStr($message):$message;
		$message  = date("Y-m-d H:i:s",time()).": ".$message."\n";		
		$this->log($file, $message);
	}
	/**
	 * 数组转换we字符串
	 * @param unknown_type $array
	 * @return Ambigous <string, mixed>
	 */
	protected function arrToStr($array){
		$str = "";
		if(is_array($array)){
			$str .= "array({array_val})";
			$strArr = array();
			foreach($array as $key=>$val){				
				if(is_array($val)){
					$strArr[] = $key."=>".$this->arrToStr($val);
				}else{
					$strArr[] = $key."=>".(string)$val;
				}
			}
			$str = str_replace("{array_val}", implode(",", $strArr), $str);
		}
		return $str;
	}
	/**
	 * 错误日志
	 * @param unknown_type $data
	 * @throws Exception
	 */
	public function errLog($data){		
		if(!$this->_errFile){
			throw new Exception("can't find error log file");
		}		
		$this->mulLog($this->_errFile,$data);	 
	}
	/**
	 * 访问日志
	 * @param unknown_type $data
	 * @throws Exception
	 */
	public function accLog($data){		
		if(!$this->_errFile){
			throw new Exception("can't find access log file");
		}
		$this->mulLog($this->_accFile, $data);
	}
	/**
	 * 调试日志
	 * @param unknown_type $data
	 * @throws Exception
	 */
	public function debLog($data){		
		if(!$this->_debFile){
			throw new Exception("can't find access log file");
		}
		$this->mulLog($this->_debFile, $data);
	}
	/**
	 * 成功日志
	 * @param unknown_type $data
	 */
	public function sucLog($data){
		if(!$this->_sucFile){
			throw new Exception("can't find success log file");
		}
		$this->mulLog($this->_sucFile, $data);
	}
	/**
	 * 普通写日志
	 * @param unknown_type $file
	 * @param unknown_type $msg
	 */
	public function writeLog($file,$msg){
		$this->mulLog($file, $msg);
	}
}